﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using MySql.Data.MySqlClient;

using System.Data.SQLite;
namespace LuZhouLaoJiao
{
    public partial class DeleteBatch : Form
    {
        public DeleteBatch()
        {
            InitializeComponent();
            this.toolTip1.SetToolTip(this.cbNotClose, "不关闭此窗口.");
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            Regex regexSuandu = new Regex(@"^[0-9,，.]+$");
            Regex retexRiqi = new Regex(@"^[0-9,，.]");
            if (tbSuandu.Text.ToString() != "" && !regexSuandu.IsMatch(tbSuandu.Text.ToString()))
            {
                MessageBox.Show("请指定适当的酸度范围.");
                return;
            }

            if (tbRiqi.Text.ToString() != "" && !retexRiqi.IsMatch(tbRiqi.Text.ToString())) 
            {
                MessageBox.Show("请指定适当的日期范围。");
                return;
            }
            if (!cbNotClose.Checked)
            {
                //不可见
                this.Visible = false;
            }


            string sqlPici = "";
            string sqlSuandu = "";
            string sqlJianceren = "";
            string sqlLaiyuan = "";
            string sqlRiqi = "";
            string sqlRemark = "";

            
            string sqlComm = "delete from batch_info where ";
            string sqlCount = "select count(*) from batch_info where ";

            //获得查询子句
            if (tbPici.Text.ToString().Trim() != "")
            {
                if (tbPici.Text.ToString().Contains(',') || tbPici.Text.ToString().Contains('，'))
                {
                    char[] split = new char[] { ',', '，' };
                    string[] values = tbPici.Text.ToString().Split(split);
                    if (values[0] != "" && values[1] != "")
                    {
                        sqlSuandu = "pici_id > '" + values[0] + "' and pici_id < '" + values[1] + "'";
                    }
                    if (values[0] == "" && values[1] != "")
                    {
                        sqlSuandu = "pici_id < '" + values[1] + "'";
                    }
                    if (values[0] != "" && values[1] == "")
                    {
                        sqlSuandu = "pici_id > '" + values[0] + "'";
                    }
                }
                else
                {
                    sqlPici = "pici_id like '%" + tbPici.Text.ToString() + "%'";
                }

            }
            if (tbSuandu.Text.ToString().Trim() != "")
            {
                if (tbSuandu.Text.ToString().Contains(',') || tbSuandu.Text.ToString().Contains('，'))
                {
                    char[] split = new char[] { ',', '，' };
                    string[] values = tbSuandu.Text.ToString().Split(split);
                    if (values[0] != "" && values[1] != "")
                    {
                        sqlSuandu = "suandu > " + values[0] + " and suandu < " + values[1];
                    }
                    if (values[0] == "" && values[1] != "")
                    {
                        sqlSuandu = "suandu < " + values[1];
                    }
                    if (values[0] != "" && values[1] == "")
                    {
                        sqlSuandu = "suandu > " + values[0];
                    }
                }
                else
                {
                    sqlSuandu = "suandu = " + tbSuandu.Text.ToString();
                }

            }
            if (tbJianceren.Text.ToString() != "")
            {
                sqlJianceren = "jianceren like '%" + tbJianceren.Text.ToString() + "%'";
            }
            if (tbLaiyuan.Text.ToString() != "")
            {
                sqlLaiyuan = "laiyuan like '%" + tbLaiyuan.Text.ToString() + "%'";
            }
            if (tbRiqi.Text.ToString() != "")
            {
                string riqi = tbRiqi.Text.ToString();
                if (riqi.Contains("."))
                {
                    riqi = riqi.Replace('.', '-');
                }
                if (riqi.Contains(',') || riqi.Contains('，'))
                {
                    char[] split = new char[] { ',', '，' };
                    string[] values = riqi.Split(split);
                    if (values[0] != "" && values[1] != "")
                    {
                        sqlRiqi = "riqi > '" + values[0] + "' and suandu < '" + values[1] + "'";
                    }
                    if (values[0] == "" && values[1] != "")
                    {
                        sqlRiqi = "riqi < '" + values[1] + "'";
                    }
                    if (values[0] != "" && values[1] == "")
                    {
                        sqlRiqi = "riqi > '" + values[0] + "'";
                    }
                }
                else
                {
                    sqlRiqi = "riqi = '" + riqi + "'";
                }
            }
            if (tbRemark.Text.ToString() != "")
            {
                sqlRemark = "beizhu like '%" + tbRemark.Text.ToString() + "%'";
            }

            //组合成查询语句
            if (sqlPici != "")
            {
                sqlComm += sqlPici + " and ";
                sqlCount += sqlPici + " and ";
            }
            if (sqlSuandu != "")
            {
                sqlComm += sqlSuandu + " and ";
                sqlCount += sqlSuandu + " and ";
            }
            if (sqlJianceren != "")
            {
                sqlComm += sqlJianceren + " and ";
                sqlCount += sqlJianceren + " and ";
            }
            if (sqlLaiyuan != "")
            {
                sqlComm += sqlLaiyuan + " and ";
                sqlCount += sqlLaiyuan + " and ";
            }
            if (sqlRiqi != "")
            {
                sqlComm += sqlRiqi + " and ";
                sqlCount += sqlRiqi + " and ";
            }
            if (sqlRemark != "")
            {
                sqlComm += sqlRemark + " and ";
                sqlCount += sqlRemark + " and ";
            }
            sqlComm = sqlComm.Substring(0, sqlComm.Length - 4);
            sqlCount = sqlCount.Substring(0, sqlCount.Length - 4);
            sqlComm += ";";
            sqlCount += ";";
            //MessageBox.Show(sqlComm);

            //删除
            //Global.dbUser = Global.getRegeditValue("localDBUser");
            //Global.dbPwd = Global.getRegeditValue("localDBPwd");
            //Global.database = Global.getRegeditValue("localDatabase");
            //Global.localDBIP = Global.getRegeditValue("localIP");
            //string strCon = "server = " + Global.localDBIP + "; user id = " + Global.dbUser + "; password = " + Global.dbPwd + "; database = " + Global.database + "; persist security info = false";
            //MySqlConnection conn = new MySqlConnection(strCon);
            //MySqlCommand comm = new MySqlCommand();
            //comm.Connection = conn;
            //comm.CommandText = sqlCount;
            string strCon = "server = " + Global.localDBIP + "; user id = " + Global.dbUser + "; password = " + Global.dbPwd + "; database = " + Global.database + "; persist security info = false";

            SQLiteConnection conn = new SQLiteConnection("Data Source=./data.db");
            SQLiteCommand comm = new SQLiteCommand();
            //MessageBox.Show("sqlite");
            conn.Open();
            comm = conn.CreateCommand();
            comm.CommandText = sqlCount;

            try
            {
                conn.Open();
                int i = Convert.ToInt32(comm.ExecuteScalar());
                if (i > 0)
                {
                    DialogResult result = MessageBox.Show("确定删除这" + i + "条记录？", "删除记录", MessageBoxButtons.YesNo);
                    if (result == DialogResult.Yes)
                    {
                        comm.CommandText = sqlComm;
                        int rowCount = comm.ExecuteNonQuery();
                        if (rowCount > 0)
                        {
                            MessageBox.Show(rowCount + "条记录被删除.");
                        }
                        else
                        {
                            MessageBox.Show("记录未能成功删除.");
                        }
                    }
                    else
                    {
                        MessageBox.Show("已取消删除");
                    }

                }
                else
                {
                    MessageBox.Show("要删除的记录不存在！");
                }

            }
            catch (System.Exception ex)
            {
                MessageBox.Show("删除记录出现错误(DB)：" + ex.Message);

            }
            finally
            {
                conn.Close();
            }

        }
    }
}
